package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.mapper.DeptMapper;
import com.qk.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;
    //添加部门
    @Override
    public void add(Dept dept) {
        //补全时间属性
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        //调用Mapper方法添加
        deptMapper.insert(dept);
    }
    //查询部门
    @Override
    public PageResult<Dept> list(String name, Integer status, Integer page, Integer pageSize) {
        //1.设置PageHelper参数分页
        PageHelper.startPage(page,pageSize);
        //2.调用Mapper方法进行查询
        List<Dept> deptlist = deptMapper.selectList(name,status);
        //3.解析并封装结果
        Page<Dept> p = (Page<Dept>)deptlist;
        return new PageResult<>(p.getTotal(),p.getResult());
    }
    //根据id查询部门
    @Override
    public Dept getById(Integer id) {
        //调用Mapper查询部门
        return deptMapper.selectById(id);
    }
    //修改部门
    @Override
    public void update(Dept dept) {
        //1.补全更新时间属性
        dept.setUpdateTime(LocalDateTime.now());
        //2.调用Mapper执行更新
        deptMapper.updateById(dept);
    }
    //删除部门
    @Override
    public void deleteById(Integer id) {
        //调用Mapper执行删除
        deptMapper.deleteById(id);
    }

}
